package cn.uin.domain.shared.adapter.repository;

/**
 * @ClassName SimpleOrder
 * @Description:简单排序规则实现类
 * @Author: Administrator
 * @CreateDate: 2025/11/14 17:36
 * @Version: 1.0
 */
public class SimpleOrder implements ISort.Order{

	private final ISort.Direction direction;
	private final String property;

	/**
	 * 构造函数
	 * @param direction 排序方向
	 * @param property 属性名
	 */
	public SimpleOrder(ISort.Direction direction, String property) {
		if (property == null || property.trim().isEmpty()) {
			throw new IllegalArgumentException("Property must not be null or empty");
		}
		this.direction = direction != null ? direction : ISort.Direction.ASC;
		this.property = property;
	}

	@Override
	public String getProperty() {
		return property;
	}

	@Override
	public ISort.Direction getDirection() {
		return direction;
	}

	@Override
	public boolean isAscending() {
		return direction.isAscending();
	}

	@Override
	public boolean isDescending() {
		return direction.isDescending();
	}

	@Override
	public ISort.Order withProperty(String property) {
		return new SimpleOrder(direction, property);
	}

	@Override
	public ISort.Order withDirection(ISort.Direction direction) {
		return new SimpleOrder(direction, property);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!(obj instanceof SimpleOrder)) return false;
		SimpleOrder that = (SimpleOrder) obj;
		return direction == that.direction && property.equals(that.property);
	}

	@Override
	public int hashCode() {
		int result = direction.hashCode();
		result = 31 * result + property.hashCode();
		return result;
	}

	@Override
	public String toString() {
		return property + ": " + direction;
	}

}
